package com.haoyeteng.sxbd.modular.business.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.haoyeteng.sxbd.core.base.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.haoyeteng.sxbd.core.base.model.ResponseResult;
import com.haoyeteng.sxbd.core.base.warpper.BaseControllerWarpper;
import com.haoyeteng.sxbd.core.common.annotion.Permission;
import com.haoyeteng.sxbd.core.common.constant.factory.ConstantFactory;
import com.haoyeteng.sxbd.core.common.constant.factory.PageFactory;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.haoyeteng.sxbd.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.haoyeteng.sxbd.modular.business.model.Auth;
import com.haoyeteng.sxbd.modular.business.service.IAuthService;
import com.haoyeteng.sxbd.modular.business.warpper.AuthWarpper;
import com.mysql.fabric.xmlrpc.base.Array;
import net.sf.jsqlparser.statement.select.Select;

/**
 * 资质认证控制器
 *
 * @author haoyeteng
 * @Date 2019-05-17 17:40:42
 */
@Controller
@RequestMapping("/auth")
public class AuthController extends BaseController {

    private String PREFIX = "/business/auth/";

    @Autowired
    private IAuthService authService;

    /**
     * 跳转到资质认证首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "auth.html";
    }

    /**
     * 跳转到添加资质认证
     */
    @RequestMapping("/auth_add")
    public String authAdd() {
        return PREFIX + "auth_add.html";
    }

    /**
     * 跳转到修改资质认证
     */
    @RequestMapping("/auth_update/{authId}")
    public String authUpdate(@PathVariable Integer authId, Model model) {
        Auth auth = authService.selectById(authId);
        model.addAttribute("item",auth);
        LogObjectHolder.me().set(auth);
        return PREFIX + "auth_edit.html";
    }

    /**
     * 获取资质认证列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    @Permission
    public ResponseResult<Object> list(String userName, Integer authState,Integer mappingGrade,
    	String province,String city,String area,String startTime,String endTime) {
    	ResponseResult<Object> result = new ResponseResult<Object>();
    	Page<Auth> page=new PageFactory<Auth>().defaultPage();
    	List<Map<String,Object>> selectAuthList=authService.selectAuthList(page, userName, authState, mappingGrade, province, city, area, startTime, endTime);
    	result.setData(new AuthWarpper(selectAuthList).warp());
 		result.setCount(page.getTotal());
     	result.setCode(SUCCESS_TIP.getCode());
     	result.setMsg(SUCCESS_TIP.getMessage());
 		return result;
    }

    /**
     *  跳转到审核页面或者跳转到查看页面
     * @param authId
     * @param model
     * @return
     */
    @RequestMapping(value="/auth_review/{type}/{authId}")
    public String authReview(@PathVariable Integer authId,@PathVariable Integer type,Model model) {
    	Map<String, Object> selectMap = authService.selectMap(new EntityWrapper<Auth>().eq("auth_id",authId));
    	List<Map<String,Object>> list=new ArrayList<>();
    	list.add(selectMap);
    	Object authList =new AuthWarpper(list).warp();
    	model.addAttribute("authList",authList);
    	if(type==1) {
    		return PREFIX+"auth_review.html";
    	}else {
    		return PREFIX+"auth_detail.html";
    	}
    }
    
    /**
     * 资质认证审核
     * @param auth
     * @return
     */
    @RequestMapping(value="/review")
    @ResponseBody
    @Permission
    public 	Object review(Auth auth) {
    	auth.setUpdateTime(new Date());
    	boolean flag = authService.updateById(auth);
    	if(flag) {
    		if(auth.getAuthState() == 1) {
    			ConstantFactory.me().toSendUserMsg("USER_AUTH", "您的资质认证已通过审核！", auth.getUserId());
    		}
    		if(auth.getAuthState() == 2) {
    			ConstantFactory.me().toSendUserMsg("USER_AUTH", "由于“"+auth.getRejectReason()+"”，您的资质认证未能通过审核，请重新认证！", auth.getUserId());
    		}
    	}
    	return SUCCESS_TIP ;
    }
    
    
    /**
     * 新增资质认证
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @Permission
    public Object add(Auth auth) {
        authService.insert(auth);
        return SUCCESS_TIP;
    }

    /**
     * 删除资质认证
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @Permission
    public Object delete(@RequestParam Integer authId) {
        authService.deleteById(authId);
        return SUCCESS_TIP;
    }

    /**
     * 修改资质认证
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @Permission
    public Object update(Auth auth) {
        authService.updateById(auth);
        return SUCCESS_TIP;
    }

    /**
     * 资质认证详情
     */
    @RequestMapping(value = "/detail/{authId}")
    @ResponseBody
    public Object detail(@PathVariable("authId") Integer authId) {
        return authService.selectById(authId);
    }
}
